package com.duff.utils;

import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.jeff.utils.AppUtil;
import com.jeff.utils.ToastUtil;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Handler mHandler = new Handler(Looper.getMainLooper());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.bt1).setOnClickListener(this);
        findViewById(R.id.bt2).setOnClickListener(this);
        findViewById(R.id.bt_test_thread).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt1:
                testToastUtil();
                break;
            case R.id.bt2:
                testAppUtil();
                break;
            case R.id.bt_test_thread:
                startActivity(new Intent(this, ThreadTestActivity.class));
                break;
        }
    }

    private void testToastUtil() {
        ToastUtil.show(this, "第一条Toast");

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                ToastUtil.show(MainActivity.this, "第二条Toast", Toast.LENGTH_LONG);
            }
        }, 500);
    }

    private void testAppUtil() {
        new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        while (true) {
                            try {
                                Thread.sleep(2000);

                                Log.d("dingfeng", "isApplicationInBackground:" + AppUtil.isApplicationInBackground(MainActivity.this));
                                Log.d("dingfeng", "isAppProcessBackground:" + AppUtil.isAppProcessBackground(MainActivity.this));
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
        ).start();
    }

}
